package com.danke.pay.demo;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.danke.lib.pay.DKPay;
import com.danke.lib.pay.DKPayLog;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DKPay.debug(true);

        // 微信支付
        findViewById(R.id.btn_wxpay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxpay();
            }
        });

        // 支付宝
        findViewById(R.id.btn_alipay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alipay();
            }
        });

        // 招行一网通
        findViewById(R.id.btn_cmb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cmbpay();
            }
        });
    }

    /**
     * 微信
     */
    private void wxpay() {
        try {
            JSONObject wx = new JSONObject();
            wx.put("payType", "1");
            JSONObject credential = new JSONObject();
            credential.put("appId", "");
            credential.put("partnerId", "");
            credential.put("prepayId", "");
            credential.put("nonceStr", "");
            credential.put("timeStamp", "");
            credential.put("packageValue", "");
            credential.put("sign", "");
            wx.put("credentialMap", credential);
            DKPay.launch(this, wx.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 支付宝
     */
    private void alipay() {
        try {
            JSONObject wx = new JSONObject();
            wx.put("payType", "2");
            JSONObject credential = new JSONObject();
            credential.put("orderInfo", "");
            wx.put("credentialMap", credential);
            DKPay.launch(this, wx.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 招行一网通
     */
    private void cmbpay() {
        try {
            JSONObject cmb = new JSONObject();
            cmb.put("payType", "3");
            JSONObject credential = new JSONObject();
            JSONObject requestData = new JSONObject("{\n" +
                    "\t\t\t\t\"version\": \"1.0\",\n" +
                    "\t\t\t\t\"sign\": \"558418c28b944b274bed327b2bc97fbdd37e8a70e9db9552c25e8d413b320bca\",\n" +
                    "\t\t\t\t\"signType\": \"SHA-256\",\n" +
                    "\t\t\t\t\"reqData\": {\n" +
                    "\t\t\t\t\t\"dateTime\": \"20190119194844\",\n" +
                    "\t\t\t\t\t\"branchNo\": \"0010\",\n" +
                    "\t\t\t\t\t\"merchantNo\": \"630316\",\n" +
                    "\t\t\t\t\t\"date\": \"20190119\",\n" +
                    "\t\t\t\t\t\"orderNo\": \"Charge1383909T1547898523\",\n" +
                    "\t\t\t\t\t\"amount\": \"50.00\",\n" +
                    "\t\t\t\t\t\"expireTimeSpan\": \"30\",\n" +
                    "\t\t\t\t\t\"payNoticeUrl\": \"https://notify.pingxx.com/notify/charges/ch_nPCyXPiPKy1OHGG0uPvfvnXP\",\n" +
                    "\t\t\t\t\t\"payNoticePara\": \"ch_nPCyXPiPKy1OHGG0uPvfvnXP\",\n" +
                    "\t\t\t\t\t\"clientIP\": \"99.12.43.61\",\n" +
                    "\t\t\t\t\t\"cardType\": \"\",\n" +
                    "\t\t\t\t\t\"subMerchantNo\": \"\",\n" +
                    "\t\t\t\t\t\"subMerchantName\": \"\",\n" +
                    "\t\t\t\t\t\"subMerchantTPCode\": \"\",\n" +
                    "\t\t\t\t\t\"subMerchantTPName\": \"\",\n" +
                    "\t\t\t\t\t\"payModeType\": \"\",\n" +
                    "\t\t\t\t\t\"agrNo\": \"6\",\n" +
                    "\t\t\t\t\t\"merchantSerialNo\": \"6\",\n" +
                    "\t\t\t\t\t\"userID\": \"6\",\n" +
                    "\t\t\t\t\t\"lon\": \"30.949505\",\n" +
                    "\t\t\t\t\t\"lat\": \"50.949506\",\n" +
                    "\t\t\t\t\t\"riskLevel\": \"\",\n" +
                    "\t\t\t\t\t\"extendInfo\": \"\",\n" +
                    "\t\t\t\t\t\"extendInfoEncrypType\": \"RC4\"\n" +
                    "\t\t\t\t}\n" +
                    "\t\t\t}");
            credential.put("jsonRequestData", requestData.toString());
            credential.put("ChannelUrl", "https://netpay.cmbchina.com/netpayment/BaseHttp.dll?MB_EUserPay");
            credential.put("channelVersion", "2.0");
            cmb.put("credentialMap", credential);
            DKPayLog.d(cmb.toString());
            DKPay.launch(this, cmb.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == DKPay.REQUEST_CODE_PAY) {
                if (data == null) {
                    return;
                }
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                String result = bundle.getString("pay_result", "");
                /* 处理返回值
                 * "success" - payment succeed
                 * "fail"    - payment failed
                 * "cancel"  - user canceld
                 * "invalid" - payment plugin not installed
                 */
                String errorMsg = bundle.getString("error_msg", ""); // 错误信息
                String extraMsg = bundle.getString("extra_msg", ""); // 错误信息
//                DKLog.e(result + ", " + errorMsg + ", " + extraMsg);
                /* result + ", " + errorMsg + ", " + extraMsg
                 * success, ,
                 * fail, channel_returns_fail,
                 * cancel, user_cancelled,
                 */
                String msg = "";
                switch (result) {
                    case "success":
                        msg = "支付成功";
                        break;
                    case "fail":
                        msg = errorMsg;
                        break;
                    case "cancel":
                        msg = "支付取消";
                        break;
                }
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
            }
        }
    }

}
